﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using HWR_Ants.Tools;
using HWR_Ants.Classes.Obsolete_2;

namespace HWR_Ants.Classes.Obsolete
{
    [Obsolete]
    class CoordinatedTour
    {
        String[] ids;
        Point[] points;
        PointWithDistances[] pointElements;
        string[] permutations;
        SortedList<string,double> calculatedTours;
        string bestTourPerm;

        public string BestTourPerm
        {
            get { return bestTourPerm; }
            set { bestTourPerm = value; }
        }
        double bestTourDistance;

        public double BestTourDistance
        {
            get { return bestTourDistance; }
            set { bestTourDistance = value; }
        }

        public CoordinatedTour(SortedList<string, Point> sortedListOfCoordinates)
        {
            ids = new String[sortedListOfCoordinates.Keys.Count];
            points = new Point[sortedListOfCoordinates.Keys.Count];
            pointElements = new PointWithDistances[sortedListOfCoordinates.Keys.Count];
            int counter = 0;
            bestTourDistance = -1;
            bestTourPerm = String.Empty;

            foreach (string key in sortedListOfCoordinates.Keys)
            {
                pointElements[counter] = new PointWithDistances(key, sortedListOfCoordinates);
                counter++;
            }

            IList<System.Drawing.Point> lc = sortedListOfCoordinates.Values;
            DistanceTable dt = new DistanceTable(lc);



            /*
            Permutation p = new Permutation(sortedListOfCoordinates.Keys.Count, sortedListOfCoordinates.Keys.ToArray<string>());

            permutations = p.SeperatedPerms;

            calculatedTours = new SortedList<string, double>();

            calculateTours();
            */

            Ant a = new Ant("1",pointElements);
        }
            
        public void calculateTours()
        {
            foreach (string element in permutations)
            {
                double value = calculateSingleTour(element);
                calculatedTours.Add(element,value);
                if (bestTourDistance > value || bestTourDistance < 0)
                {
                    bestTourDistance = value;
                    bestTourPerm = element;
                }
            }
        }

        private double calculateSingleTour(string perm)
        {
            double ret = 0;
            string[] points = perm.ToSeperatedString(" ");
            string endpoint = points[0];

            for (int i = 0; i < points.Length - 1; i++ )
            {
                ret += getPointWithDistancesFromKey(points[i]).getDistance(points[i + 1]);
            }
            ret += getPointWithDistancesFromKey(points[points.Length - 1]).getDistance(endpoint);
            return ret;
        }

        private PointWithDistances getPointWithDistancesFromKey(string id)
        {
            foreach (PointWithDistances p in pointElements)
            {
                if (p.Id == id)
                    return p;
            }
            return null;
        }
    }
}
